package com.fanshuai.event;

import com.fanshuai.lifecycle.aware.DemoEventPublisher;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 通过EventPublisher发送事件，通知Listener处理
 * EventPublisher默认实现为ApplicationContext，ApplicationContext通过ApplicationEventMulticaster发送事件
 *
 * ApplicationEventMulticaster为观察者模式容器，提供addListener, removeListener, multicastEvent等功能
 */
@Component
public class EventSender {
    @Resource
    private DemoEventPublisher demoEventPublisher;

    public void sendEvent() {
        FanshuaiEvent event = new FanshuaiEvent("fanshuai");
        demoEventPublisher.getApplicationEventPublisher().publishEvent(event);
    }
}
